/*
Pachube Client.
 
 
 Based on: 
 
 Web client
 
 This sketch connects to a website (http://www.google.com)
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 18 Dec 2009
 by David A. Mellis
 
 */

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  
  0x90, 0xA2, 0xDA, 0x00, 0xF6, 0x3A };
const String pachubeKey = "sc1W7fiShkAo-5m7U6xCaYN1rMESNog2K5FkEyTT41kc5uVv3PKqVaTHa0I1-g5JeT7BnbmuAxZACaxTk-yJHpFohPF9MSvRFzy9IfD2xcQGcLyHMe-wuHwoXUzOaehV7ZBFdf6PZZo82-t2aDRQXA";

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // start the serial library:
  Serial.begin(9600);
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }

  //////////////////////////
  //print network settings//
  //////////////////////////

  outputNetworkInfo("My localIP address: ", Ethernet.localIP());
  outputNetworkInfo("My subnetMask address: ", Ethernet.subnetMask());
  outputNetworkInfo("My gatewayIP address: ", Ethernet.gatewayIP());
  outputNetworkInfo("My dnsServerIP address: ", Ethernet.dnsServerIP());

  // give the Ethernet shield a second to initialize:
  delay(1000);

  String payload = "{\"version\":\"1.0.0\",\"datastreams\":[{\"id\":\"DiningRoomTemp\",\"current_value\":\"20\"}]}";
  sendData(payload);
}

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  while (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    Serial.println("connecting...");
  }
  
  delay(2000);
}

void sendData(String payload)
{
  // if you get a connection, report back via serial:
  if (client.connect("api.pachube.com", 80)) {
    Serial.println("connected");

    // calculate the length of the sensor reading in bytes:
    int thisLength = payload.length();

    // Make a HTTP request:
    client.println("PUT /v2/feeds/42739 HTTP/1.1");
    client.println("Host: api.pachube.com");         
    client.println("X-PachubeApiKey: " + pachubeKey);
    client.print("Content-Length: ");    
    client.println(thisLength, DEC);
    client.println("Content-Type: application/json");  
    client.println("Connection: close\n");

    //sendLine("Content-Type: application/json; charset=utf-8");  
    client.println(payload);  
    client.println();
    client.println();
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  
}

void outputNetworkInfo(String description, IPAddress myAddress)
{
  Serial.print(description);
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(myAddress[thisByte], DEC);
    Serial.print("."); 
  }
  Serial.println();
}


